package com.tengju.motivation.application.activity;

import com.tengju.motivation.application.shared.ApplicationException;
import com.tengju.motivation.domain.model.performance.*;
import com.tengju.motivation.domain.shared.DateUtil;
import com.tengju.motivation.domain.shared.PerformanceObjectiveType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 李大仙
 * @date: 2021/7/22 4:55 下午
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MotivationActivityConfigService {

//    private final MotivationActivityConfigRepository motivationActivityConfigRepository;
    private final PerformanceObjectiveRepository performanceObjectiveRepository;

//    @Value("#{${import.template.map}}")
//    private Map<String, String> importTemplateMap;

    public int save(Long activityIdVal, String title, Integer objectType, String remark, LocalDateTime startTime, LocalDateTime endTime, String staffName) {
//        if (activityIdVal == null) {
//            MotivationActivityConfig motivationActivityConfig =
//                    MotivationActivityConfigFactory.create(title, objectType, remark, startTime, endTime, staffName);
//            return motivationActivityConfigRepository.save(motivationActivityConfig);
//        } else {
//            MotivationActivityConfig activityConfig = getByActivityId(activityIdVal);
//            activityConfig.edit(title, objectType, remark, startTime, endTime, staffName);
//            if (activityConfig.getStartTime() != startTime || activityConfig.getEndTime() != endTime) {
//                // 修改时间
////                performanceObjectiveRepository.updateByRelationId(
////                        activityConfig.getActivityId().getValue(),
////                        DateUtil.getSecondLong(startTime),
////                        DateUtil.getSecondLong(endTime));
//            }
//            return motivationActivityConfigRepository.edit(activityConfig);
//        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public int remove(Long activityIdVal) {
        MotivationActivityConfig activityConfig = getByActivityId(activityIdVal);
//        performanceObjectiveRepository.removeByRelationId(activityConfig.getActivityId().getValue());
//        return motivationActivityConfigRepository.remove(activityConfig.getActivityId());
        return 0;
    }

    public MotivationActivityConfig getByActivityId(Long activityIdVal) {
//        MotivationActivityConfig motivationActivityConfig =
//                motivationActivityConfigRepository.getByActivityId(new MotivationActivityConfigId(activityIdVal));
//        if (motivationActivityConfig == null) {
//            throw ApplicationException.error("活动管理不存在 id：" + activityIdVal);
//        }
        return null;
    }

    public String getActivityPerformanceObjectiveTemplate() {
//        return importTemplateMap.get("activityPerformanceObjectiveTemplate");
        return "";
    }

    @Transactional(rollbackFor = Exception.class)
    public void importPerformanceObjective(List<String[]> rows, Long activityId, String operatorName) {
        MotivationActivityConfig activityConfig = getByActivityId(activityId);
        PerformanceObjectiveTypeEnum performanceObjectiveTypeEnum = MotivationPerformanceObjectiveTypeConvert.convert(activityConfig.getObjectType());

        Map<String, Integer> map = new HashMap<>();

        List<PerformanceObjective> performanceObjectives = new ArrayList<>();
        int lineNumber = 1;
        for (String[] cells : rows) {
            lineNumber++;
            String secIdCodeVal = cells[0];
            if (StringUtils.isEmpty(secIdCodeVal)) {
                throw ApplicationException.error("第" + lineNumber + "行SEC用户ID为空");
            }
            String performanceObjectiveName = cells[1];
            if (StringUtils.isEmpty(performanceObjectiveName)) {
                throw ApplicationException.error("第" + lineNumber + "行目标名称为空");
            }
            if (!PerformanceObjectiveType.getTargetDescList().contains(performanceObjectiveName)) {
                throw ApplicationException.error("第" + lineNumber + "行目标名称不正确");
            }
            String performanceObjectiveVal = cells[2];
            if (StringUtils.isEmpty(performanceObjectiveVal)) {
                throw ApplicationException.error("第" + lineNumber + "行目标值为空");
            }
            String key = secIdCodeVal + "_" + performanceObjectiveName;
            if (map.containsKey(key)) {
                throw ApplicationException.error("第" + lineNumber + "行与第" + map.get(key) + "行配置重复");
            } else {
                map.put(key, lineNumber);
            }
            Long startTime = DateUtil.getSecondLong(activityConfig.getStartTime());
            Long endTime = DateUtil.getSecondLong(activityConfig.getEndTime());
            PerformanceObjectiveType performanceObjectiveType = PerformanceObjectiveType.getByTargetDesc(performanceObjectiveName);
//            performanceObjectives.add(PerformanceObjectiveFactory.create(Long.valueOf(secIdCodeVal),
//                    performanceObjectiveTypeEnum.getCode(),
//                    performanceObjectiveType.getCode(),
//                    performanceObjectiveType.getDesc(),
//                    "",
//                    Integer.valueOf(performanceObjectiveVal),
//                    startTime,
//                    endTime,
//                    activityConfig.getActivityId().getValue(),
//                    operatorName
//            ));
        }
//        if (!CollectionUtils.isEmpty(performanceObjectives)) {
//            performanceObjectiveRepository.removeByRelationId(activityConfig.getActivityId().getValue());
//            performanceObjectiveRepository.batchAdd(performanceObjectives);
//        }
    }
}
